package net.lzzy.controller;

import net.lzzy.entity.Article;
import net.lzzy.entity.Comment;
import net.lzzy.entity.User;
import net.lzzy.service.ArticleService;
import net.lzzy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/back")
public class BackController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    //文章管理页面
    @GetMapping("/article")
    public String articleManager(Model model) {
        List<Article> articles = articleService.findAll();
        model.addAttribute("articles",articles);
        return "/back/article_manager";
    }

    //文章删除
    @GetMapping("/article/delete/{id}")
    public String deleteArticle(@PathVariable("id") Integer id){
        articleService.deleteById(id);
        return "redirect:/back/article";
    }

    //文章修改页面
    @GetMapping("/article/edit/{id}")
    public String editArticlePage(@PathVariable("id") Integer id, Model model){
        model.addAttribute("article", articleService.findArticleById(id));
        return "/back/article_edit";
    }

    //文章新增页面
    @GetMapping("/article/add")
    public String addArticlePage(){
        return "/back/article_edit";
    }

    //文章修改发布
    @PostMapping("/article/publish/{id}")
    public String editArticle(@PathVariable("id") Integer id, @RequestParam("title") String title, @RequestParam("category") String category, @RequestParam("content") String content){
        articleService.updateArticleById(id, title, category, content);
        return "redirect:/article/"+id;
    }

    //文章新增发布
    @PostMapping("/article/publish")
    public String addArticle(@RequestParam("title") String title, @RequestParam("category") String category, @RequestParam("content") String content){
        Article article =  articleService.addArticle(title, category, content);
        return "redirect:/article/"+article.getId();
    }
//评论
    @GetMapping("/comment")
    public String commentManager(Model model) {
        List<Comment> comments = commentService.findAll();
        model.addAttribute("comments",comments);
        return "/back/article_pinglun";
    }
    //评论删除
    @GetMapping("/comment/delete/{id}")
    public String deletecomment(@PathVariable("id") Integer id){
        commentService.deleteById(id);
        return "redirect:/back/comment";
    }
    //用户
    @GetMapping("/user")
    public String  userManager(Model model) {
        List<User> comments =commentService.findUser();
        model.addAttribute("users",comments);
        System.out.println("123");
        return "/back/article_user";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        commentService.deleteUser(id);
        return "redirect:/back/user";
    }

//    //文章修改页面
//    @GetMapping("/comment/edit/{id}")
//    public String editCommentPage(@PathVariable("id") Integer id, Model model){
//        model.addAttribute("comment", commentService.findCommentById(id));
//        return "/back/article_plxg";
//    }
//
//    //文章新增页面
//    @GetMapping("/article/add")
//    public String addCommentPage(){
//        return "/back/article_plxg";
//    }
//
//    //文章修改发布
//    @PostMapping("/article/publish/{id}")
//    public String editComment(@PathVariable("id") Integer id, @RequestParam("a_id") Integer a_id, @RequestParam("author") String author, @RequestParam("content") String content){
//        commentService.updateCommentById(id, a_id, author, content);
//        return "redirect:/comment/"+id;
//    }
//
//    //文章新增发布
//    @PostMapping("/article/publish")
//    public String addArticle(@RequestParam("a_id") Integer a_id, @RequestParam("author") String author, @RequestParam("content") String content){
//        Comment comment =  commentService.addArticle(a_id, author, content);
//        return "redirect:/comment/"+comment.getId();
//    }

}
